# include <reg52.h>
# include <stdio.h>
# define uint unsigned int 
# define uchar unsigned char 
sbit dq=P3^2;
sbit wei=P3^6;
sbit duan=P3^7;
uint temp;
float f_temp;
uint warn_11=270;
uint warn_12=250;
uint warn_h1=300;
uint warn_h2=320;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,
                    0x6d,0x7d,0x07,0x7f,0x6f};
void delay (uchar time)
{
     uchar x,y;
	 for (x=time;x>0;x--)
	    for (y=110;y>0;y--);
}
void dsrest (void)
{
     uchar i;
	 dq=0;
	 i=103;
	 while (i>0)i--;
	 dq=1;
	 i=4;
	 while (i>0)i--;
}
bit tempreadbit (void)
{
    uchar i;
	bit dat;
	dq=0;
	i++;
	dq=1;
	i++;
	i++;
	dat=dq;
	i=8;
	while (i>0)i--;
	return (dat);
}
uchar tempread (void)
{
      uchar i,j,dat;
	  dat=0;
	  for (i=1;i<=8;i++)
	  {
	      j=tempreadbit ();
		  dat=(j<<7)|(dat>>1);
	  }
	  return (dat);
}
void tempwritebyte (uchar dat)
{
     uchar i;
	 uchar j;
	 bit testb;
	 for (j=1;j<=8;j++)
	 {
	     testb=dat&0x01;
		 dat=dat>>1;
		 if (testb)
		 {
		    dq=0;
			i++;i++;
			dq=1;
			i=8;while (i>0)i--;
		 }
		 else 
		 {
		    dq=0;
			i=8;while (i>0)i--;
			dq=1;
			i++;i++;
		 }
	 }
}
uint get_temp ()
{
     int a,b;
     dsrest ();
	 delay (1);
	 tempwritebyte (0xcc);
	 tempwritebyte (0xbe);
	 a=tempread ();
	 b=tempread ();
	 temp=b;
	 temp<<=8;
	 temp=temp|a;
	 f_temp=temp*0.0625;
	 temp=f_temp*10+0.5;
	 f_temp=f_temp+0.5;
	 return temp;
}
void tempchange (void)
{
     dsrest ();
	 delay (1);
	 tempwritebyte (0xcc);
	 tempwritebyte (0x44);
}
void display (uchar num,uchar dat)
{
     uchar i;
	 duan=0;
	 P0=table[dat];
	 duan=1;
	 duan=0;
	 wei=0;
	 i=0xff;
	 i=i&(~((0x01)<<(num)));
	 P0=i;
	 wei=1;
	 wei=0;
	 delay (1);
}
void dis_temp (uint t)
{
     uchar i;
	 i=t/100;
	 display (0,i);
	 i=t%100/10;
	 display (1,i+10);
	 i=t%100%10;
	 display (2,i);
}
void main ()
{
     uchar buff[4],i;
	 duan=0;
	 while (1)
	 {
	       tempchange ();
		   for (i=10;i>0;i--)
		   {
		       dis_temp (get_temp ());
		   }
		   sprintf (buff,"%f",f_temp);
		   for (i=10;i>0;i--)
		   {
		       dis_temp (get_temp ());
		   }
		   for (i=10;i>0;i--)
		   {
		       dis_temp (get_temp ());
		   }
	 }
}
